/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include <stdio.h>
#include "bus.h"

int main(int argc, char *argv[])
{
	z_arg_buffer ab = z_arg_buffer_init(1, "-_-bus [OPTIONS]");
	z_arg_buffer_sign(&ab, "-_- bus", "0.1-alpha", "Oliver Katz", 2012);

	if (z_init_args(&ab, argc, argv))
		return 0;

	z_log log = z_log_init_with_file(stdout);

	z_ipc_server srv = z_ipc_server_init_global(8950);
	z_ipc_server_connect(&srv);

	z_list progs = z_list_init(1024);

	z_msg readymsg = z_msg_init(0, "-_- bus is ready");
	z_msg_dump(&log, &readymsg);
	z_msg_release(readymsg);
	
	while(1)
	{
		z_data req = z_ipc_server_wait(&srv, NULL, NULL);
		if (req.tid == Z_TYPEID_STR)
		{
			printf("'%s'\n", (char *)req.src);
			z_bus_parse_programs((char *)req.src, &progs);
		}
		char *resstr = z_bus_list_programs(progs);
		z_data res = z_data_wrap(resstr, Z_TYPEID_STR, strlen(resstr)*sizeof(char));
		z_ipc_server_reply(&srv, &res);
		sleep(1);
	}

	z_ipc_server_disconnect(&srv);
	z_ipc_server_release(srv);
	z_log_release(log);
	z_arg_buffer_release(ab);
	return 0;
}
